package UILayer;
import ControlLayer.*;
import java.util.Scanner;
import ModelLayer.*;

/**
 * Write a description of class SaleUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaleUI
{
    private Scanner reader;
    private OrderController orderController;
    private PersonController personController;
    public SaleUI()
    {
        orderController = new OrderController();
        reader = new Scanner(System.in);
        personController = new PersonController();
        start();
    }
    
    /**
     * Prints the menu on the screen.
     */
    private void writeMenu()
    {
        System.out.println("\f*** Sale Menu ***");
        System.out.println("(1)  Make sale without Person");
        System.out.println("(2)  Make sale WITH Person");        
        System.out.println("");
        System.out.println("(0)  Return to MainMenu");
        System.out.println("");
        System.out.print("make choise: ");
    
    }
    
    
    /**
     * Loops until the user chose an option.
     */
    public void start()
    {
        boolean stop = false;
        while(!stop) {
            writeMenu();
            int i = UIInput.inputInt();
            if(i == 1) 
            {
                System.out.println("\f- Make sale without Person -");
                makeSaleNoPerson();
            }
            if(i == 2) 
            {
                System.out.println("\f- Make sale WITH Person -");
                makeSaleWithPerson();
            }            
            if(i == 0) 
            {
                stop = true;
            }
        }
        
    }
    
    private void makeSaleNoPerson()
    {
       System.out.print("Enter id on order: ");
       int id = UIInput.inputInt();
       int orderIndex = id-1;
        
       try
       {
            
            if(orderController.findOrder(id) != null)
            {
                int index = 0;
                System.out.println("\fOrder id: " + id);
                System.out.println("- Items -");
                while(index < orderController.listAllOrder().get(orderIndex).getPartOrders().size())
                {
                    double price = orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getItem().getSellingPrice();
                    int amount = orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getAmount();
                    double totalPrice = price * amount;
                    double totalpriceNoVAT = totalPrice - (totalPrice * orderController.listAllOrder().get(orderIndex).getVAT());
                    System.out.println("Barcode: " + orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getItem().getBarcode());
                    System.out.println("Name: " + orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getItem().getName());
                    System.out.println("Price: " + price);
                    System.out.println("Amount: " + amount);
                    System.out.println("");
                        
                    System.out.println("Price with No VAT: " + totalpriceNoVAT);
                    System.out.println("Price with VAT   : " + totalPrice);
                    System.out.println("----------------------");
                    index++;
                }                
                System.out.println("");
                System.out.println("Total without VAT:              Total with VAT:");
                System.out.println(orderController.listAllOrder().get(orderIndex).getWithoutVAT() + "                           " + orderController.listAllOrder().get(orderIndex).getTotalPrice());
                System.out.println("Status on order: " + orderController.listAllOrder().get(id-1).getStatus());
                if(orderController.listAllOrder().get(id-1).getStatus().equals("canceled"))
                {
                    System.out.println("Order is canceled");
                }
            }
       }
        
        catch(Exception e)
       {
            System.out.println("No match found!");
       }        
       

       try
       {
           if(orderController.findOrder(id) != null)
           {
       
               System.out.print("Payment. Press 1 to pay. | Press 2 to abort: ");
               int pay = UIInput.inputInt();
       
               if(pay == 1)
               {
                   if(orderController.listAllOrder().get(orderIndex).getPartOrders().size() < 1)
                   {
                       System.out.println("You have no items. You have to abort");
                       orderController.listAllOrder().get(orderIndex).setStatus("canceled");
                   }
            
                   else if(orderController.listAllOrder().get(id-1).getStatus().equals("canceled"))
                   {
                       System.out.println("This order is canceled. You cant pay this");
                    }
            
                    else if(orderController.listAllOrder().get(id-1).getStatus().equals("delivered"))
                    {
                        System.out.println("This order is paid");
                    }
                    else
                    {
                        int index = 0;
                        while(index < orderController.listAllOrder().get(orderIndex).getPartOrders().size())
                        {
                            int amountSold = orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getAmount();
                            orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getItem().setAmountInStock(orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getItem().getAmountInStock()-amountSold);
                            index++;
                        }
                        System.out.println("Paying.");
                        System.out.println("Paying..");
                        System.out.println("Paying...");         
                        System.out.println("Complete.");
                        try
                        {
                            orderController.findOrder(id).setStatus("delivered");
                        }
                        catch(Exception e)
                        {
                            System.out.println(e.getMessage());
                        }
                    }
                }
                else
                {
                    try
                    {
                        orderController.deleteOrder(id);

                        System.out.println("Order canceled");
                    }
                    catch (Exception e)
                    {
                        System.out.println(e.getMessage());
                    }
                }
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
       back();
    }
    
        private void makeSaleWithPerson()
        {
            System.out.print("Enter id on order: ");
            int id = UIInput.inputInt();
            int orderIndex = id-1;
        
       try
       {
            
            if(orderController.findOrder(id) != null && orderController.findOrder(id).getStatus().equals("stock"))
            {
                int index = 0;
                   
                while(index < orderController.listAllOrder().get(orderIndex).getPartOrders().size())
                {
                     double price = orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getItem().getSellingPrice();
                     double amount = orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getAmount();
                     double totalPrice = price * amount;
                     double totalpriceNoVAT = totalPrice - (totalPrice * orderController.listAllOrder().get(orderIndex).getVAT());
                     System.out.println("Barcode: " + orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getItem().getBarcode());
                     System.out.println("Name: " + orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getItem().getName());
                     System.out.println("Price: " + price);
                     System.out.println("Amount: " + amount);
                     System.out.println("");
                        
                     System.out.println("Price with No VAT: " + totalpriceNoVAT);
                     System.out.println("Price with VAT   : " + totalPrice);
                     index++;
                }
                System.out.println("");
                System.out.println("Total without VAT:              Total with VAT:");
                System.out.println(orderController.listAllOrder().get(orderIndex).getWithoutVAT() + "                           " + orderController.listAllOrder().get(orderIndex).getTotalPrice());
                if(orderController.listAllOrder().get(id-1).getStatus().equals("canceled"))
                {
                    System.out.println("ORDER IS CANCELED");
                }
                
                try
                {
                    System.out.println("Type buyers id(phonenumber): ");
                    int phoneNumber = UIInput.inputInt();
                
                    orderController.setBuyerInfo(id, phoneNumber);
       
                    System.out.print("\n");   
          
        System.out.println("Person added to order: " + orderController.getBuyerInfo(id).getName());
                        
        Person newPerson = orderController.getBuyerInfo(id);
        String personType = personController.checkObjectType(newPerson);
        
        if(personType.equals("PrivateCustomer"))
        {
            System.out.println("Person phone number: " + ((RegisteredCustomer)newPerson).getPhoneNumber()); 
        }
        if(personType.equals("BusinessCustomer"))
        {
            System.out.println("Person phone number: " + ((RegisteredCustomer)newPerson).getPhoneNumber());
        }
        if(personType.equals("WarehouseEmployee"))
        {
            System.out.println("Person phone number: " + ((Employee)newPerson).getPhoneNumber());
        }
        if(personType.equals("Manager"))
        {
            System.out.println("Person phone number: " + ((Employee)newPerson).getPhoneNumber());
        }
        if(personType.equals("Salesman"))
        {
            System.out.println("Person phone number: " + ((Employee)newPerson).getPhoneNumber());
        }
                    
   
           
           
                    System.out.print("\n");              
                    System.out.print("Payment. Press 1 to pay. | Press 2 to abort: ");
                    int pay = UIInput.inputInt();
       
                    if(pay == 1)
                    {
                        if(orderController.listAllOrder().get(orderIndex).getPartOrders().size() < 1)
                        {
                            System.out.println("You have no items. You have to abort");
                            orderController.listAllOrder().get(orderIndex).setStatus("canceled");
                        }
                        else
                        {
                            index = 0;
                            while(index < orderController.listAllOrder().get(orderIndex).getPartOrders().size())
                            {
                                int amountSold = orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getAmount();
                                orderController.listAllOrder().get(orderIndex).getPartOrders().get(index).getItem().setAmountInStock(amountSold);
                                index++;
                            }
                
                            System.out.println("Paying.");
                            System.out.println("Paying..");
                            System.out.println("Paying...");         
                            System.out.println("Complete.");

                            try
                            {
                                orderController.findOrder(id).setStatus("delivered");
                            }
                            catch(Exception e)
                            {
                                System.out.println(e.getMessage());
                            }                
                        }
                    }
                    else
                    {
                        try
                        {
                            orderController.deleteOrder(orderIndex);
                            System.out.println("Order canceled");
                        }
                        catch (Exception e)
                        {
                            System.out.println(e.getMessage());
                        }
                    }
                }
                catch(Exception e)
                {
                    System.out.println(e.getMessage());
                }
            }
            else
            {
                System.out.println("Order is canceled or already delivered");
            }
       }
        
        catch(Exception e)
       {
            System.out.println("No match found!");
       }        
       back();
    }    
    
     /**
     * Wait.
     */
    private void back()
    {
        System.out.println("");
        System.out.print("Press 'enter' to get back to menu");
        String i = reader.nextLine();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
